<template>
  <div class="app-container">

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <router-link :to="'/property/office/examine/approve?propertyId='+this.$route.query.propertyId" v-if="this.$route.query.propertyId!=undefined" class="link-type">
          <el-button icon="el-icon-back" size="mini">
            返 回
          </el-button>
        </router-link>

        <router-link :to="'/property/office/examine/approve'" v-else class="link-type">
          <el-button icon="el-icon-back" size="mini">
            返 回
          </el-button>
        </router-link>
      </el-col>
    </el-row>
    <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
      <el-tab-pane label="未审核" name="0"/>
      <el-tab-pane label="审核中" name="1"/>
      <el-tab-pane label="已通过" name="2"/>
      <el-tab-pane label="已撤销" name="3"/>
      <el-tab-pane label="已驳回" name="4"/>
    </el-tabs>
    <el-table v-loading="loading" :data="recordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="${comment}" align="center" prop="id" v-if="false"/>

      <el-table-column label="发起时间" align="center" prop="createTime" />

      <el-table-column label="审批类目" align="center" prop="approveName" />

      <el-table-column label="发起的审批标题" align="center" prop="approveTitle" />

      <el-table-column label="审批类型" align="center" prop="approveType">
        <template slot-scope="scope">
          <div>
            <el-tag v-if="scope.row.approveType === 0" style="cursor: pointer" :type="''">报销类</el-tag>
            <el-tag v-if="scope.row.approveType === 1" style="cursor: pointer" :type="''">采购类</el-tag>
          </div>
        </template>
      </el-table-column>

<!--      <el-table-column label="发起人微信" align="center" prop="nickName" />-->


<!--      <el-table-column label="用户头像" align="center">-->
<!--        <template slot-scope="scope">-->
<!--          <a :href="scope.row.linkLogo" style="color: #42b983" target="_blank">-->
<!--            <img :src="scope.row.faceUrl" alt="点击打开" class="el-avatar">-->
<!--          </a>-->
<!--        </template>-->
<!--      </el-table-column>-->

      <el-table-column label="申请人姓名" align="center" prop="userName" />

<!--      <el-table-column label="部门名称" align="center" prop="deptName" />-->

      <el-table-column label="清算金额" align="center" prop="settlementMoney">
        <template slot-scope="scope">
          {{scope.row.settlementMoney+"元"}}
        </template>
      </el-table-column>


      <el-table-column label="审批的状态" align="center" prop="state" >
        <template slot-scope="scope">
          <el-popover v-if="scope.row.state===1" placement="right" width="300" trigger="hover">
            <el-row v-loading="loadingState" >
              <el-col>
                <el-row>
                  审批步骤：{{record.stateName}}
                </el-row>
                <el-row>
                  审批人：{{record.userName}}
                </el-row>
              </el-col>
            </el-row>
            <el-tag style="cursor: pointer" :type="'warning'" slot="reference" @mouseenter.native="getState(scope.row.id)">审批中</el-tag>
          </el-popover>
          <el-popover v-if="scope.row.state===0" placement="right" width="300" trigger="hover">
            <el-row v-loading="loadingState" >
              <el-col>
                <el-row>
                  审批步骤：{{record.stateName}}
                </el-row>
                <el-row>
                  审批人：{{record.userName}}
                </el-row>
              </el-col>
            </el-row>
            <el-tag style="cursor: pointer" :type="''" slot="reference" @mouseenter.native="getState(scope.row.id)">待审批</el-tag>
          </el-popover>
          <el-popover v-if="scope.row.state===2" placement="right" width="300" trigger="hover">
            <el-row v-loading="loadingState" >
              <el-col>
                <el-row>
                  审批步骤：{{record.stateName}}
                </el-row>
                <el-row>
                  审批人：{{record.userName}}
                </el-row>
              </el-col>
            </el-row>
            <el-tag style="cursor: pointer" :type="'success'" slot="reference" @mouseenter.native="getState(scope.row.id)">审核完成</el-tag>
          </el-popover>

          <el-popover v-if="scope.row.state===3" placement="right" width="300" trigger="hover">
            <el-row v-loading="loadingState" >
              <el-col>
                <el-row>
                  审批步骤：{{record.stateName}}
                </el-row>
                <el-row>
                  审批人：{{record.userName}}
                </el-row>
              </el-col>
            </el-row>
            <el-tag style="cursor: pointer" :type="'danger'" slot="reference" @mouseenter.native="getState(scope.row.id)">审核撤销</el-tag>
          </el-popover>

          <el-popover v-if="scope.row.state===4" placement="right" width="300" trigger="hover">
            <el-row v-loading="loadingState" >
              <el-col>
                <el-row>
                  审批步骤：{{record.stateName}}
                </el-row>
                <el-row>
                  审批人：{{record.userName}}
                </el-row>
              </el-col>
            </el-row>
            <el-tag style="cursor: pointer" :type="'danger'" slot="reference" @mouseenter.native="getState(scope.row.id)">审核拒绝</el-tag>
          </el-popover>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="220px">
        <template slot-scope="scope">

          <el-button size="mini" type="text" icon="el-icon-edit" @click="show(scope.row) ">查看清算物品</el-button>

          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['module:record:edit']">审批</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['module:record:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getList" />

    <!-- 添加或修改审批申请记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-form-item label="审批类目" prop="approveName">
          <el-input v-model="form.approveName" placeholder="请输入审批类目id" size="mini" disabled/>
        </el-form-item>

        <el-form-item label="审批标题" prop="approveTitle">
          <el-input v-model="form.approveTitle" placeholder="请输入发起的审批标题" size="mini" disabled/>
        </el-form-item>

        <el-form-item label="申请人昵称" prop="userName">
          <el-input v-model="form.nickName" placeholder="请输入发起人 微信用户id" size="mini" disabled/>
        </el-form-item>

        <el-form-item label="申请人姓名" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入申请人姓名" size="mini" disabled/>
        </el-form-item>

        <el-form-item label="部门名称" prop="deptName">
          <el-input v-model="form.deptName" placeholder="请输入小区/物业/公司名称/部门名称" size="mini" disabled/>
        </el-form-item>

        <el-form-item label="清算金额" prop="settlementMoney">
          <el-input v-model="form.settlementMoney" placeholder="请输入清算金额" size="mini" disabled>
            <template slot="append">元</template>
          </el-input>
        </el-form-item>

        <el-form-item label="审批状态" prop="approveType">
          <el-radio-group v-model="form.state"  style="width:95%;" size="mini">
            <el-radio :value="0" :label="0">待审批</el-radio>
            <el-radio :value="1" :label="1">审批中</el-radio>
            <el-radio :value="2" :label="2">审核完成</el-radio>
            <el-radio :value="3" :label="3">撤销审批</el-radio>
          </el-radio-group>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" size="mini" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel" size="mini">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="title" :visible.sync="showInfo" width="500px" append-to-body>
      <el-row v-for="(item, index) in info" :key="item.id" style="border: solid 1px;border-radius:5px;margin-top: 10px">
        <el-form ref="item" :model="item" :rules="rules" label-width="80px">
          <el-row style="margin-top: 10px">
            <el-col :span="11">
              <el-form-item label="物品名称" prop="goodName">
                <el-input v-model="item.goodName" placeholder="请输入物品名称" size="mini" disabled/>
              </el-form-item>
            </el-col>
            <el-col :span="11">
              <el-form-item label="采购数量" prop="count">
                <el-input v-model="item.count" placeholder="请输入采购数量" size="mini" disabled>
                  <template slot="append">个</template>
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="11">
              <el-form-item label="采购单价" prop="singlePrice">
                <el-input v-model="item.singlePrice" placeholder="请输入采购单价" size="mini" disabled>
                  <template slot="append">元</template>
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="11">
              <el-form-item label="采购总价" prop="totalPrice">
                <el-input v-model="item.totalPrice" placeholder="请输入总价格" size="mini" disabled>
                  <template slot="append">元</template>
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <!--        <el-row>-->
          <!--          <el-col :span="11">-->
          <!--            <el-form-item label="物品规格" prop="specs">-->
          <!--              <el-input v-model="info.specs" placeholder="请输入规格" size="mini" disabled/>-->
          <!--            </el-form-item>-->
          <!--          </el-col>-->
          <!--          <el-col :span="11">-->
          <!--            <el-form-item label="要求信息" prop="requirementInfo">-->
          <!--              <el-input v-model="info.requirementInfo" placeholder="请输入要求信息" size="mini" disabled/>-->
          <!--            </el-form-item>-->
          <!--          </el-col>-->
          <!--        </el-row>-->
          <el-row>
            <el-col :span="22">
              <el-form-item label="备注信息" prop="remark">
                <el-input v-model="item.remark" placeholder="请输入备注信息" size="mini" disabled/>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </el-row>

    </el-dialog>
  </div>
</template>

<script>
  import { listRecord, getRecord, delRecord, addRecord, updateRecord, exportRecord } from "@/api/module/examine/record";
  import Template from "@/views/module/wx/template";
  import {getInfo} from "@/api/module/examine/info";
  import {getAdminRecord} from "../../../api/module/examine/adminRecord";

  export default {
    name: "Record",
    components: {Template},
    data() {
      return {
        activeName:'1',
        // 按钮loading
        buttonLoading: false,
        // 遮罩层
        loading: true,
        loadingState:false,
        // 导出遮罩层
        exportLoading: false,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        propertyId:undefined,
        // 总条数
        total: 0,
        // 审批申请记录表格数据
        recordList: {
          stateName:undefined,
          userName:undefined,
        },
        record: [],
        showInfo:false,
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          approveId:undefined,
          state:1,
        },
        // 表单参数
        form: {},
        info:{},
        // 表单校验
        rules: {
        }
      }
    },
    created() {
      this.queryParams.approveId = this.$route.query.approveId ? this.$route.query.approveId : undefined;
      this.getList();
      this.propertyId=this.$route.query.propertyId ? this.$route.query.propertyId : undefined;

    },
    methods: {
      /** 查询审批申请记录列表 */
      getList() {
        this.loading = true;
        listRecord(this.queryParams).then(response => {
          this.recordList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      handleClick(val){
        console.log('handleClick',val.name);
        this.activeName = val.name;
        this.queryParams.state = parseInt(val.name);
        this.getList();
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          approveId: undefined,
          approveTitle: undefined,
          approveType: undefined,
          userId: undefined,
          userName: undefined,
          deptName: undefined,
          settlementMoney: undefined,
          createTime: undefined,
          state: undefined,
          approveName:undefined,
          nickName:undefined,
        };
        this.resetForm("form");
      },
      show(row){
        this.loading = true;
        this.reset();
        const id = row.id || this.ids;
        getInfo(id).then(response => {
          this.loading = false;
          console.log(response)
          this.info = response;
          this.showInfo=true;
          this.title = "查看物品清单";
        });
      },
      getState(id){
        this.record={
          stateName:undefined,
          userName:undefined,
        }
        this.loadingState = true;
        getAdminRecord(id).then(response => {
          this.loadingState = false;
          this.record = response.data;
        });
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id);
        this.single = selection.length!==1;
        this.multiple = !selection.length;
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加审批申请记录";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.loading = true;
        this.reset();
        const id = row.id || this.ids;
        getRecord(id).then(response => {
          this.loading = false;
          this.form = response.data;
          this.open = true;
          this.title = "修改审批申请记录";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.buttonLoading = true;
            if (this.form.id != null) {
              updateRecord(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }).finally(() => {
                this.buttonLoading = false;
              });
            } else {
              addRecord(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }).finally(() => {
                this.buttonLoading = false;
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除审批申请记录编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.loading = true;
          return delRecord(ids);
        }).then(() => {
          this.loading = false;
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有审批申请记录数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportRecord(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
      }
    }
  };
</script>
